<script setup lang="ts">
import { ref } from 'vue'

let addFlag = ref<boolean>(false)

let markIptRef = ref()

const markList = ref<Array<any>>([])

let hallText = ref<string>('')

// ----------------------------

const emit = defineEmits<{
  okList: [list: Array<any>]
}>()

let props = withDefaults(defineProps<{ list: Array<any> }>(), {
  list: () => [],
})

const list = toRef(props.list)
markList.value = list.value

watch(() => ({...props}), (newVal, oldVal) => {
  // console.log('props=', props.list)
  markList.value = props.list
})

// ----------------------------

const okText = () => {
  markList.value.push(hallText.value)
  emit('okList', markList.value)
  // console.log('markList=', markList.value)

  addFlag.value = false
  hallText.value = ''
}

const add = () => {
  addFlag.value = true
  nextTick(() => {
    markIptRef.value.focus()
  })
}

const del = (target: any) => {
  markList.value = markList.value.filter(item => item != target)
  emit('okList', markList.value)
}
</script>

<template>
  <div class="ipt-div">
    <a-tag v-for="(item, index) in markList" :key="index" class="ipt-tag">
      {{ item }}
      <CloseOutlined class="ipt-tag-del" @click="del(item)" />
    </a-tag>
  </div>

  <div style="width: 320px; line-height: 32px">
    <a-input
      placeholder="按回车键添加"
      style="width: 280px; height: 30px"
      ref="markIptRef"
      v-show="addFlag"
      v-model:value="hallText"
      @keyup.enter="okText"
    ></a-input>

    <a @click="add" style="margin-left: 5px;" v-show="!addFlag">添加</a>

  </div>
</template>

<style lang="less" scoped>
input {
  width: 320px;
}

/deep/ .ant-select-selector {
  width: 320px;
}

.ipt-div {
  border: 1px solid #d9d9d9;
  min-height: 32px;
  width: 320px;
  border-radius: 6px;
  padding: 0 3px;
}

.ipt-tag {
  margin: 3px;
  font-size: 14px;
  padding: 0 4px 0 8px;
  border: 0;
  background: #f5f5f5;
  cursor: pointer;
  height: 24px;
  line-height: 24px;
}

.ipt-tag-del {
  font-size: 12px;
  line-height: 24px;
  color: #878787;
  margin-left: 5px;
}
</style>
